#include "hardwidget.h"
#include "ui_hardwidget.h"
#include "../CheckInfor/hardinfor.h"
#include <QHeaderView>

QVector<QPair<Yzty::MODECODE,Yzty::ITEMCODE>> HardWidget::ShowTools;
QVector<QSet<int>> HardWidget::ToolMapItem;       //执行的功能对应的item编号

HardWidget::HardWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HardWidget)
{
    ui->setupUi(this);
    //创建model
    this->_cpumodel=new QStandardItemModel();
    this->_memorymodel=new QStandardItemModel();
    this->_graphmodel=new QStandardItemModel();
    this->_soundmodel=new QStandardItemModel();
    this->_diskmodel=new QStandardItemModel();
    this->_networkmodel=new QStandardItemModel();
    this->_mousemodel=new QStandardItemModel();
    this->_keyboardmodel=new QStandardItemModel();
    this->_usermodel=new QStandardItemModel();

    //加载model
    ui->cpu_table->setModel(_cpumodel);
    ui->cpu_table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->memory_table->setModel(_memorymodel);
    ui->memory_table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->graph_table->setModel(_graphmodel);
    ui->graph_table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->sound_table->setModel(_soundmodel);
    ui->sound_table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->disk_table->setModel(_diskmodel);
    ui->disk_table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->network_table->setModel(_networkmodel);
    ui->network_table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->mouse_table->setModel(_mousemodel);
    ui->mouse_table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->keyboard_table->setModel(_keyboardmodel);
    ui->keyboard_table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->user_table->setModel(_usermodel);
    ui->user_table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    HardWidget::ToolMapItem.resize(int(Yzty::HardToolCode::NUMS));
    for(int i=0;i<int(Yzty::HardToolCode::NUMS);++i){
        UpdateData(Yzty::HardToolCode(i));
        HardWidget::ShowTools.push_back(QPair<int,int>(int(Yzty::ToolCode::Hard),i));
        HardWidget::ToolMapItem[i].insert(int(Yzty::ToolCode::Hard)*Yzty::ToolMapItemHashCode+i);
    }

}

HardWidget::~HardWidget()
{
    delete ui;
}

void HardWidget::SetCurrentWidget(Yzty::MODECODE code)
{
    ui->stackedWidget->setCurrentIndex(code);
    UpdateData(Yzty::HardToolCode(code));
}

void HardWidget::UpdateData(Yzty::HardToolCode code)
{
    if(code==Yzty::HardToolCode::Cpu)
        UpdateCpuInfor();
    else if(code==Yzty::HardToolCode::MotherBoard)
        UpdateMoboInfor();
    else if(code==Yzty::HardToolCode::Memory)
        UpdateMemoryInfor();
    else if(code==Yzty::HardToolCode::GraphCard)
        UpdateGraphInfor();
    else if(code==Yzty::HardToolCode::SoundCard)
        UpdateSoundInfor();
    else if(code==Yzty::HardToolCode::HardDisk)
        UpdateDiskInfor();
    else if(code==Yzty::HardToolCode::Battery)
        UpdateBatteryInfor();
    else if(code==Yzty::HardToolCode::BlueTooth)
        UpdateBlueToothInfor();
    else if(code==Yzty::HardToolCode::NetCard)
        UpdateNetWorkInfor();
    else if(code==Yzty::HardToolCode::Mouse)
        UpdateMouseInfor();
    else if(code==Yzty::HardToolCode::KeyBoard)
        UpdateKeyBoardInfor();
    else if(code==Yzty::HardToolCode::OsAndHost)
        UpdateOsInfor();
    else if(code==Yzty::HardToolCode::User)
        UpdateUserInfor();
}

void HardWidget::UpdateCpuInfor()
{
    /*
        "名称",0
        "制造商",1
        "处理器ID",2
        "核心ID",3
        "核心数",4
        "当前频率",5
        "BogoMIPS",6
        "架构",7
        "型号",8
        "字长",9
        "数据存储模式"10
*/
    const std::vector<Yzty::BaceInfor*>& infors=HardInfor::GetInstance()->GetNormalCheck(Yzty::HardToolCode::Cpu);
    //预览表格进行更新
    this->_cpumodel->setHorizontalHeaderLabels({"名称","制造商","频率","架构"});
    if(this->_cpumodel->rowCount()==0)
    {
        for(size_t i=0;i<infors.size();++i)
        {
            QList<QStandardItem*> items;
            items.push_back(new QStandardItem(infors[i]->GetItem(0)));  //名称
            items.push_back(new QStandardItem(infors[i]->GetItem(1)));  //制造商
            items.push_back(new QStandardItem(infors[i]->GetItem(5)));  //频率
            items.push_back(new QStandardItem(infors[i]->GetItem(7)));  //架构
            this->_cpumodel->appendRow(items);
        }
    }


    ui->cpu_edit->clear();
    for(size_t i=0;i<infors.size();++i)
    {
        ui->cpu_edit->appendHtml(QString("<b>逻辑处理器%1</b>").arg(i));
        for(int j=0;j<Yzty::cpuInfor::keywords.size()-1;++j)
            ui->cpu_edit->appendHtml(QString("<pre><b>%1:&#9;&#9;&#9;</b>").arg(Yzty::cpuInfor::keywords[j])
                                     +infors[i]->GetItem(int(j))+"</pre>");

        ui->cpu_edit->appendHtml(QString("<pre><b>%1:&#9;&#9;</b>")
                                 .arg(Yzty::cpuInfor::keywords[Yzty::cpuInfor::keywords.size()-1])
                +infors[i]->GetItem(int(Yzty::cpuInfor::keywords.size()-1))+"</pre>");

        ui->cpu_edit->appendHtml("</br></br>");
    }

}

void HardWidget::UpdateMoboInfor()
{
    /*
     * moboinfor
        "名称",0
        "制造商",1
        "版本",2
        "类型",3
        "序列号",5
        "特性"6

      *bios
        "制造商",0
        "版本",1
        "ROM",2
        "运行内存",3
        "特性"4

       *mesolt
        "位置",0
        "卡槽数量",1
        "最大容量"2

     */
    const std::vector<Yzty::BaceInfor*>& infors=HardInfor::GetInstance()->GetNormalCheck(Yzty::HardToolCode::MotherBoard);
    size_t itemnums=infors.size();

    ui->mobo_edit->clear();
    for(size_t i=0;i<itemnums;++i)
    {
        if(i==0)
            ui->mobo_edit->appendHtml("<b>主板</b>");
        else
            ui->mobo_edit->appendHtml(QString("<b>主板_%1</b>").arg(i));

        for(int j=0;j<Yzty::moboInfor::keywords.size();++j)
            ui->mobo_edit->appendHtml(QString("<pre><b>%1:&#9;&#9;&#9;</b>").arg(Yzty::moboInfor::keywords[j])
                                      +infors[i]->GetItem(j)+"</pre>");
        ui->mobo_edit->appendHtml("</br></br>");

        if(i==0)
            ui->mobo_edit->appendHtml("<b>Bios</b>");
        else
            ui->mobo_edit->appendHtml(QString("<b>Bios_%1</b>").arg(i));

        for(int j=0;j<Yzty::biosInfor::keywords.size();++j)
            ui->mobo_edit->appendHtml(QString("<pre><b>%1:&#9;&#9;&#9;</b>").arg(Yzty::biosInfor::keywords[j])
                                      +dynamic_cast<Yzty::moboInfor*>(infors[i])->_bios.GetItem(j)+"</pre>");
        ui->mobo_edit->appendHtml("</br></br>");

        if(i==0)
            ui->mobo_edit->appendHtml("<b>内存插槽</b>");
        else
            ui->mobo_edit->appendHtml(QString("<b>内存插槽_%1</b>").arg(i));

        for(int j=0;j<Yzty::mesoltInfor::keywords.size();++j)
            ui->mobo_edit->appendHtml(QString("<pre><b>%1:&#9;&#9;&#9;</b>").arg(Yzty::mesoltInfor::keywords[j])
                                      +dynamic_cast<Yzty::moboInfor*>(infors[i])->_msolts.GetItem(j)+"</pre>");
        ui->mobo_edit->appendHtml("</br></br>");
    }

}

void HardWidget::UpdateMemoryInfor()
{
    /*
        "名称",0
        "制造商",1
        "型号",2
        "大小",3
        "序列号",4
        "速度",5
        "数据位宽",6
        "配置频率",7
        "插槽类型"8
    */
    this->_memorymodel->setHorizontalHeaderLabels({"名称","制造商","速度","大小"});
    const std::vector<Yzty::BaceInfor*>& infors=HardInfor::GetInstance()->GetNormalCheck(Yzty::HardToolCode::Memory);
    if(this->_memorymodel->rowCount()==0)
    {
        for(size_t i=0;i<infors.size();++i)
        {
            QList<QStandardItem*> items;
            items.push_back(new QStandardItem(infors[i]->GetItem(0)));  //名称
            items.push_back(new QStandardItem(infors[i]->GetItem(1)));  //制造商
            items.push_back(new QStandardItem(infors[i]->GetItem(5)));  //速度
            items.push_back(new QStandardItem(infors[i]->GetItem(3)));  //大小

            _memorymodel->appendRow(items);
        }
    }

    size_t itemnums=infors.size();
    ui->memory_edit->clear();
    for(size_t i=0;i<itemnums;++i)
    {
        ui->memory_edit->appendHtml(QString("<b>%1</b>").arg(infors[i]->GetItem(0)));
        for(int j=0;j<Yzty::memoryInfor::keywords.size();++j)
            ui->memory_edit->appendHtml(QString("<pre><b>%1:&#9;&#9;&#9;</b>").arg(Yzty::memoryInfor::keywords[j])
                                        +infors[i]->GetItem(int(j))+"</pre>");

        ui->memory_edit->appendHtml("</br></br>");
    }
}

void HardWidget::UpdateGraphInfor()
{
    /*
        "名称",0
        "制造商",1
        "型号",2
        "版本",3
        "位宽",4
        "功能",5
        "接口",6
        "频率",7
        "描述",8
        "驱动模块",9
        "当前分辨率",10
        "最大分辨率",11
        "最小分辨率",12
        "IO端口",13
        "配置信息"14
     */
    const std::vector<Yzty::BaceInfor*>& infors=HardInfor::GetInstance()->GetNormalCheck(Yzty::HardToolCode::GraphCard);
    this->_graphmodel->setHorizontalHeaderLabels({"名称","制造商","型号"});
    if(this->_graphmodel->rowCount()==0)
    {
        for(size_t i=0;i<infors.size();++i)
        {
            QList<QStandardItem*> items;
            items.push_back(new QStandardItem(infors[i]->GetItem(0)));  //名称
            items.push_back(new QStandardItem(infors[i]->GetItem(1)));  //制造商
            items.push_back(new QStandardItem(infors[i]->GetItem(2)));  //速度

            _graphmodel->appendRow(items);
        }
    }

    size_t itemnums=infors.size();
    ui->graph_edit->clear();
    for(size_t i=0;i<itemnums;++i)
    {
        ui->graph_edit->appendHtml(QString("<b>%1</b>").arg(infors[i]->GetItem(0)));
        for(int j=0;j<Yzty::graphcardInfor::keywords.size();++j)
            ui->graph_edit->appendHtml(QString("<pre><b>%1:&#9;&#9;&#9;</b>").arg(Yzty::graphcardInfor::keywords[j])
                                        +infors[i]->GetItem(int(j))+"</pre>");

        ui->graph_edit->appendHtml("</br></br>");
    }

}

void HardWidget::UpdateSoundInfor()
{
//    "名称",0
//    "制造商",1
//    "型号",2
//    "版本",3
//    "功能",4
//    "配置",5
//    "频率",6
//    "位宽"7

    const std::vector<Yzty::BaceInfor*>& infors=HardInfor::GetInstance()->GetNormalCheck(Yzty::HardToolCode::SoundCard);
    this->_soundmodel->setHorizontalHeaderLabels({"名称","制造商"});
    if(this->_soundmodel->rowCount()==0)
    {
        for(size_t i=0;i<infors.size();++i)
        {
            QList<QStandardItem*> items;
            items.push_back(new QStandardItem(infors[i]->GetItem(0)));  //名称
            items.push_back(new QStandardItem(infors[i]->GetItem(1)));  //制造商

            _soundmodel->appendRow(items);
        }
    }

    size_t itemnums=infors.size();
    ui->sound_edit->clear();
    for(size_t i=0;i<itemnums;++i)
    {
        ui->sound_edit->appendHtml(QString("<b>%1</b>").arg(infors[i]->GetItem(0)));
        for(int j=0;j<Yzty::soundcardInfor::keywords.size();++j)
            ui->sound_edit->appendHtml(QString("<pre><b>%1:&#9;&#9;&#9;</b>").arg(Yzty::soundcardInfor::keywords[j])
                                        +infors[i]->GetItem(int(j))+"</pre>");

        ui->sound_edit->appendHtml("</br></br>");
    }

}

void HardWidget::UpdateDiskInfor()
{
//    "名称",
//    "制造商",
//    "介质",
//    "大小",
//    "接口",
//    "序列号",
//    "驱动模块",
//    "驱动文件",
//    "配置信息",
//    "硬件类型"
    const std::vector<Yzty::BaceInfor*>& infors=HardInfor::GetInstance()->GetNormalCheck(Yzty::HardToolCode::HardDisk);
    this->_diskmodel->setHorizontalHeaderLabels({"名称","制造商","介质类型","大小"});
    if(this->_diskmodel->rowCount()==0)
    {
        for(size_t i=0;i<infors.size();++i)
        {
            QList<QStandardItem*> items;
            items.push_back(new QStandardItem(infors[i]->GetItem(0)));  //名称
            items.push_back(new QStandardItem(infors[i]->GetItem(1)));  //制造商
            items.push_back(new QStandardItem(infors[i]->GetItem(2)));  //介质
            items.push_back(new QStandardItem(infors[i]->GetItem(3)));  //大小

            _diskmodel->appendRow(items);
        }
    }

    size_t itemnums=infors.size();
    ui->disk_edit->clear();
    for(size_t i=0;i<itemnums;++i)
    {
        ui->disk_edit->appendHtml(QString("<b>%1</b>").arg(infors[i]->GetItem(0)));
        for(int j=0;j<Yzty::diskInfor::keywords.size();++j)
            ui->disk_edit->appendHtml(QString("<pre><b>%1:&#9;&#9;&#9;</b>").arg(Yzty::diskInfor::keywords[j])
                                        +infors[i]->GetItem(int(j))+"</pre>");

        ui->disk_edit->appendHtml("</br></br>");
    }
}

void HardWidget::UpdateBatteryInfor()
{
    ui->battery_edit->clear();
    const std::vector<Yzty::BaceInfor*>& infors=HardInfor::GetInstance()->GetNormalCheck(Yzty::HardToolCode::Battery);
    for(size_t i=0;i<infors.size();++i)
    {
        ui->battery_edit->appendHtml(QString("<b>电池</b>"));
        for(int j=0;j<Yzty::batteryInfor::keywords.size();++j)
            ui->battery_edit->appendHtml(QString("<pre><b>%1:&#9;&#9;&#9;</b>").arg(Yzty::batteryInfor::keywords[j])
                                        +infors[i]->GetItem(int(j))+"</pre>");

        ui->battery_edit->appendHtml("</br></br>");
    }
}

void HardWidget::UpdateBlueToothInfor()
{
//    "名称",
//    "制造商",
//    "驱动",
//    "接口",
//    "速度",
//    "硬件类型",
//    "配置信息"
    ui->bluetooth_edit->clear();
    const std::vector<Yzty::BaceInfor*>& infors=HardInfor::GetInstance()->GetNormalCheck(Yzty::HardToolCode::BlueTooth);
    for(size_t i=0;i<infors.size();++i)
    {
        ui->bluetooth_edit->appendHtml(QString("<b>蓝牙</b>"));
        for(int j=0;j<Yzty::bluetoothInfor::keywords.size();++j)
            ui->bluetooth_edit->appendHtml(QString("<pre><b>%1:&#9;&#9;&#9;</b>").arg(Yzty::bluetoothInfor::keywords[j])
                                        +infors[i]->GetItem(int(j))+"</pre>");

        ui->bluetooth_edit->appendHtml("</br></br>");
    }

}

void HardWidget::UpdateNetWorkInfor()
{
    const std::vector<Yzty::BaceInfor*>& infors=HardInfor::GetInstance()->GetNormalCheck(Yzty::HardToolCode::NetCard);
    this->_networkmodel->setHorizontalHeaderLabels({"名称","制造商","类型"});
    if(this->_networkmodel->rowCount()==0)
    {
        for(size_t i=0;i<infors.size();++i)
        {
            QList<QStandardItem*> items;
            items.push_back(new QStandardItem(infors[i]->GetItem(0)));  //名称
            items.push_back(new QStandardItem(infors[i]->GetItem(1)));  //制造商
            items.push_back(new QStandardItem(infors[i]->GetItem(2)));  //类型

            _networkmodel->appendRow(items);
        }
    }

    size_t itemnums=infors.size();
    ui->network_edit->clear();
    for(size_t i=0;i<itemnums;++i)
    {
        ui->network_edit->appendHtml(QString("<b>%1</b>").arg(infors[i]->GetItem(0)));
        for(int j=0;j<Yzty::networkcardInfor::keywords.size();++j)
            ui->network_edit->appendHtml(QString("<pre><b>%1:&#9;&#9;&#9;</b>").arg(Yzty::networkcardInfor::keywords[j])
                                        +infors[i]->GetItem(int(j))+"</pre>");

        ui->network_edit->appendHtml("</br></br>");
    }
}

void HardWidget::UpdateMouseInfor()
{
    const std::vector<Yzty::BaceInfor*>& infors=HardInfor::GetInstance()->GetNormalCheck(Yzty::HardToolCode::Mouse);
    this->_mousemodel->setHorizontalHeaderLabels({"名称","制造商","型号"});
    if(this->_mousemodel->rowCount()==0)
    {
        for(size_t i=0;i<infors.size();++i)
        {
            QList<QStandardItem*> items;
            items.push_back(new QStandardItem(infors[i]->GetItem(0)));  //名称
            items.push_back(new QStandardItem(infors[i]->GetItem(1)));  //制造商
            items.push_back(new QStandardItem(infors[i]->GetItem(2)));  //类型

            _mousemodel->appendRow(items);
        }
    }

    size_t itemnums=infors.size();
    ui->mouse_edit->clear();
    for(size_t i=0;i<itemnums;++i)
    {
        ui->mouse_edit->appendHtml(QString("<b>%1</b>").arg(infors[i]->GetItem(0)));
        for(int j=0;j<Yzty::mouseInfor::keywords.size();++j)
            ui->mouse_edit->appendHtml(QString("<pre><b>%1:&#9;&#9;&#9;</b>").arg(Yzty::mouseInfor::keywords[j])
                                        +infors[i]->GetItem(int(j))+"</pre>");

        ui->mouse_edit->appendHtml("</br></br>");
    }
}

void HardWidget::UpdateKeyBoardInfor()
{
    const std::vector<Yzty::BaceInfor*>& infors=HardInfor::GetInstance()->GetNormalCheck(Yzty::HardToolCode::KeyBoard);
    this->_keyboardmodel->setHorizontalHeaderLabels({"名称","制造商","型号"});
    if(this->_keyboardmodel->rowCount()==0)
    {
        for(size_t i=0;i<infors.size();++i)
        {
            QList<QStandardItem*> items;
            items.push_back(new QStandardItem(infors[i]->GetItem(0)));  //名称
            items.push_back(new QStandardItem(infors[i]->GetItem(1)));  //制造商
            items.push_back(new QStandardItem(infors[i]->GetItem(2)));  //类型

            _keyboardmodel->appendRow(items);
        }
    }

    size_t itemnums=infors.size();
    ui->keyboard_edit->clear();
    for(size_t i=0;i<itemnums;++i)
    {
        ui->keyboard_edit->appendHtml(QString("<b>%1</b>").arg(infors[i]->GetItem(0)));
        for(int j=0;j<Yzty::keyboardInfor::keywords.size();++j)
            ui->keyboard_edit->appendHtml(QString("<pre><b>%1:&#9;&#9;&#9;</b>").arg(Yzty::keyboardInfor::keywords[j])
                                        +infors[i]->GetItem(int(j))+"</pre>");

        ui->keyboard_edit->appendHtml("</br></br>");
    }
}

void HardWidget::UpdateOsInfor()
{
//    "机器名称",
//    "制造商",
//    "系统名称",
//    "版本",
//    "系统类型",
//    "内核类型",
//    "内核版本",
//    "处理器信息",
//    "UUID"
    const std::vector<Yzty::BaceInfor*>& infors=HardInfor::GetInstance()->GetNormalCheck(Yzty::HardToolCode::OsAndHost);

    if(infors.size()==0)
        return;

    ui->os_label->setText(infors[0]->GetItem(2));
    ui->os_label->setAlignment(Qt::AlignCenter);

    ui->os_edit->clear();
    for(int i=0;i<Yzty::osInfor::keywords.size();++i)
        ui->os_edit->appendHtml(QString("<pre><b>%1:&#9;&#9;&#9;</b>").arg(Yzty::osInfor::keywords[i])
                                    +infors[0]->GetItem(i)+"</pre>");

}

void HardWidget::UpdateUserInfor()
{
//    "用户名",
//    "终端号",
//    "登陆时间",
//    "执行时间",
//    "执行命令"
    this->_usermodel->setHorizontalHeaderLabels(Yzty::userInfor::keywords);
    const std::vector<Yzty::BaceInfor*>& infors=HardInfor::GetInstance()->GetNormalCheck(Yzty::HardToolCode::User);
    if(_usermodel->rowCount()==0)
    {
        for(size_t i=0;i<infors.size();++i)
        {
            QList<QStandardItem*> items;
            for(int j=0;j<Yzty::userInfor::keywords.size();++j)
                items.push_back(new QStandardItem(infors[i]->GetItem(j)));
            _usermodel->appendRow(items);
        }
    }

    //编辑区清空
    ui->user_edit->clear();
    for(size_t i=0;i<infors.size();++i)
    {
        ui->user_edit->appendHtml(QString("<p><b>用户%1开关机记录</b></p>").arg(infors[i]->GetItem(0)));
        int j=1;
        for(auto& temp:dynamic_cast<Yzty::userInfor*>(infors[i])->_log)
        {
            ui->user_edit->appendHtml(QString("<pre><b>%1&#9;&#9;</b>").arg(j)+temp+"</pre>");
            j++;
        }
        ui->user_edit->appendHtml("</hr>");
    }

}
